package com.xboss.tv.ui;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xboss.tv.R;
import com.xboss.tv.bean.Contacts;

import java.io.IOException;

/**
 * Created by Administrator on 2017\7\31 0031.
 */

public class MusicActivity extends Activity {

    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_music);

        initUIData();

        Intent intent = getIntent();

        String url = intent.getStringExtra("url");
        String name = intent.getStringExtra("name");
        String pic = intent.getStringExtra("pic");

        ImageView image = (ImageView) findViewById(R.id.music_image_pic);
        TextView title = (TextView) findViewById(R.id.music_text_name);

        TextView back = (TextView) findViewById(R.id.music_text_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Glide.with(MusicActivity.this).load(pic).into(image);
        title.setText(name);

        // 当player对象为空时
        if (player == null) {
            player = MediaPlayer.create(MusicActivity.this, Uri
                    .parse(url));//实例化对象，通过播放本机服务器上的一首音乐
            player.setLooping(false);//设置不循环播放
        }

        play();//开始播放音乐
    }

    private void initUIData() {
        Display display = getWindowManager().getDefaultDisplay(); // 为获取屏幕宽、高
        Window window = getWindow();
        WindowManager.LayoutParams windowLayoutParams = window.getAttributes(); // 获取对话框当前的参数值
        windowLayoutParams.width = (int) (display.getWidth() * 0.3); // 宽度设置为屏幕的0.95
        windowLayoutParams.height = (int) (display.getHeight() * 0.5); // 高度设置为屏幕的0.6
    }

    //停止播放音乐方法
    private void stop() {
        // 当player对象不为空时
        if (player != null) {
            player.seekTo(0);//设置从头开始
            player.stop();//停止播放
            try {
                player.prepare();//预加载音乐
            } catch (IllegalStateException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }

    //暂停播放音乐方法
    private void pause() {
        // 当player对象正在播放时并且player对象不为空时
        if (player.isPlaying() && player != null) {
            player.pause();//暂停播放音乐
        }
    }

    //播放音乐方法
    private void play() {
        // 当player对象不为空并且player不是正在播放时
        if (player != null && !player.isPlaying()) {
            player.start();//开始播放音乐
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        play();
    }

    @Override
    protected void onPause() {
        super.onPause();
        pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stop();
        Contacts.isLoad = true;
    }
}
